﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TransFormer.BLL;

namespace TransFormer.Web.admin
{
    public partial class ProductEdit : AdminPage
    {
        private BLL.ProductType _bllPt = new ProductType();
        private BLL.Product _bllProduct = new BLL.Product();
        private log4net.ILog logger = log4net.LogManager.GetLogger(typeof(ProductEdit));
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack) return;
            int id = -1;
            if (int.TryParse(Request["productId"], out id))
            {
                var model = _bllProduct.GetModel(id);
                txtId.Text = model.ProductId.ToString();
                txtProductName.Text = model.ProductName;
                txtProductDesc.Text = model.ProductDesc;

                var list = _bllPt.GetModelList(string.Empty);
                DdlProductType.DataSource = list;
                DdlProductType.DataTextField = "TypeName";
                DdlProductType.DataValueField = "Id";
                DdlProductType.DataBind();
                DdlProductType.Items.FindByValue(model.Type).Selected = true;
                language.Items.FindByValue(model.Language.Trim()).Selected = true;

                imgSize.ImageUrl = "." + model.Size;
                imgProduct.ImageUrl = "." + model.PicUrl;

            }
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {

            Model.Product product = new Model.Product();

            try
            {
                var model = _bllProduct.GetModel(int.Parse(txtId.Text));
                if (fileProductImg.HasFile)
                {
                    if (fileProductImg.FileBytes.Length > 10 * 1024 * 1024)
                    {
                        lbResult.InnerHtml = "您上传参数文件过大，请减小文件体积后重新上传！";
                        return;
                    }
                    string productPath = Server.MapPath("../images/products/");
                    string name = Path.GetFileNameWithoutExtension(fileProductImg.FileName) + Guid.NewGuid() + ".jpg";
                    productPath += name;
                    fileProductImg.SaveAs(productPath);

                   
                    if (!string.IsNullOrEmpty(model.PicUrl))
                    {
                        string path = Server.MapPath("." + model.PicUrl);
                        try
                        {
                            File.Delete(path);
                        }
                        catch (Exception exception)
                        {
                            logger.Error(exception.Message, exception);
                        }
                    }

                    product.PicUrl = "./images/products/" + name;
                }
                if (fileProductSize.HasFile)
                {
                    if (fileProductSize.FileBytes.Length > 10 * 1024 * 1024)
                    {
                        lbResult.InnerHtml = "您上传的图片文件过大，请减小文件体积后重新上传！";
                        return;
                    }
                    string sizePath = Server.MapPath("../images/productSize/");
                    string name = Path.GetFileNameWithoutExtension(fileProductSize.FileName) + Guid.NewGuid() + ".jpg";
                    sizePath += name;
                    fileProductSize.SaveAs(sizePath);

                    if (!string.IsNullOrEmpty(model.Size))
                    {
                        string path = Server.MapPath("." + model.Size);
                        try
                        {
                            File.Delete(path);
                        }
                        catch (Exception exception)
                        {
                            logger.Error(exception.Message, exception);
                        }
                    }

                    product.Size = "./images/productSize/" + name;
                }
                product.ProductId = int.Parse(txtId.Text);
                product.ProductName = txtProductName.Text;
                product.ProductDesc = txtProductDesc.Text;
                product.Type = DdlProductType.SelectedValue;
                product.Language = language.Value;
                if (_bllProduct.Update(product))
                {
                    Response.Redirect("productlist.aspx", false);
                }
                else
                {
                    lbResult.InnerHtml = "修改产品错误！！！请联系管理员";

                }
            }
            catch (Exception exception)
            {
                logger.Error(exception.Message, exception);
                Response.Redirect("../error.html");
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {

            Response.Redirect("productlist.aspx", false);
        }
    }
}